package org.programmers.anonymous.fightrobots.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.programmers.anonymous.fightrobots.controller.IWeaponController;
import org.programmers.anonymous.fightrobots.model.IDeffense;
import org.programmers.anonymous.fightrobots.model.IWeapon;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
@RequestMapping("/weapon-service")
public class WeaponService implements BeanFactoryAware{
	
	private IWeaponController weaponController = null;
	

	@RequestMapping(value="/getWeapon", method = RequestMethod.GET)
	public @ResponseBody IWeapon receiveMessage(@RequestParam(value="weaponId", required=true) String weaponId, ModelMap model)
	{
		return weaponController.loadWeapon(Integer.valueOf(weaponId));
		
	}
	@RequestMapping(value="/getAllWeapons", method = RequestMethod.GET)
	public @ResponseBody List<String>  getAllWeapons()
	{
	List<String> weaponList=new ArrayList<String>();
    	 
     	for (IWeapon iWeapon: weaponController.getAllWeapons()){
     		weaponList.add(iWeapon.getName());	     		
     	}
     	
     	return weaponList;
		
	}
	
	/**
	* Handles request for adding a weapon
	*/
	@RequestMapping(value = "/createWeapon", method = RequestMethod.POST)
	public  void createWeapon(
	@RequestParam(value = "weaponName", required = true) String weaponName,
	@RequestParam(value = "weaponPower", required = true) Integer weaponPower,
	@RequestParam(value = "weaponHits", required = true) Integer weaponHits,
	ModelMap model) {
		weaponController.addWeapon(weaponController.createWeapon(weaponName, weaponPower, weaponHits));
	
	}
	
	@RequestMapping(value="/deleteWeapon" ,method = RequestMethod.POST)
	public @ResponseBody boolean deleteWeapon(@RequestParam(value="weaponId", required=true) String weaponId, ModelMap model){
	     	return weaponController.delete(weaponController.loadWeapon(Integer.valueOf(weaponId)));	
	}
	

	@RequestMapping(value="/editWeapon" ,method = RequestMethod.POST)
	public @ResponseBody int editWeapon(
			@RequestParam(value="weaponId", required=true) int weaponId,
			@RequestParam(value = "weaponName", required = true) String weaponName,
			@RequestParam(value = "weaponPower", required = true) Integer weaponPower,
			@RequestParam(value = "weaponHits", required = true) Integer weaponHits,
			 ModelMap model){
		
		IWeapon iweapon = weaponController.loadWeapon(weaponId);
		iweapon.setName(weaponName);
		iweapon.setHits(weaponHits);
		iweapon.setPower(weaponPower);
		return weaponController.save(iweapon);
	

	}
	
	@Override
	public void setBeanFactory(BeanFactory context) throws BeansException
	{
		weaponController = (IWeaponController) context.getBean("weaponController");
	}
}